package com.etc.controller;

import java.text.SimpleDateFormat;
import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.etc.entity.Goods;
import com.etc.service.GoodsService;
import com.etc.tools.AjaxOperateResponse;
import com.etc.tools.ResponseData;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;

@RestController
@RequestMapping("api")
@Api(value = "商品的接口", description = "处理商品操作的后端接口")
public class GoodsController {

	@Autowired
	private GoodsService goodsService;

	// begindate enddate
	@GetMapping("goods")
	@ApiOperation(value = "查询商品", httpMethod = "GET", notes = "分页加模糊查询的方法", response = ResponseData.class)
	public ResponseData<Goods> listGoods(
			@ApiParam(name = "keywords", required = true, defaultValue = "", value = "查询关键字") @RequestParam(name = "keywords", required = true, defaultValue = "") String keywords,
			@ApiParam(name = "page", required = true, defaultValue = "1", value = "页码") @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
			@ApiParam(name = "limit", required = true, defaultValue = "10", value = "每页记录数") @RequestParam(name = "limit", required = true, defaultValue = "10") Integer limit,
			@RequestParam(name = "begin", defaultValue = "2021-01-01") String begin,
			@RequestParam(name = "end", defaultValue = "") String end) {

		Date beginDate = null;
		Date endDate = null;
		ResponseData<Goods> rd = null;
		try {
			if (begin != null && !begin.isEmpty()) {
				beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(begin);
			}
			

			if (!end.isEmpty()) {
				endDate = new SimpleDateFormat("yyyy-MM-dd").parse(end);
			}

			rd = goodsService.getGoodsByLike(keywords, beginDate, endDate, page, limit);

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		return rd;
	}
	
	
	// begindate enddate
		@GetMapping("goods2")
		@ApiOperation(value = "查询商品", httpMethod = "GET", notes = "分页加模糊查询的方法", response = ResponseData.class)
		public ResponseData<Goods> listGoods2(
				@ApiParam(name = "keywords", required = true, defaultValue = "", value = "查询关键字") @RequestParam(name = "keywords", required = true, defaultValue = "") String keywords,
				@ApiParam(name = "page", required = true, defaultValue = "1", value = "页码") @RequestParam(name = "page", required = true, defaultValue = "1") Integer page,
				@ApiParam(name = "limit", required = true, defaultValue = "10", value = "每页记录数") @RequestParam(name = "limit", required = true, defaultValue = "10") Integer limit,
				@RequestParam(name = "begindate", defaultValue = "2021-01-01") String begindate,
				@RequestParam(name = "enddate", defaultValue = "") String enddate) {

			
			ResponseData<Goods> rd = null;
			try {
			
				if (enddate.isEmpty()) {
					enddate = new SimpleDateFormat("yyyy-MM-dd").format(new Date());
				}

				rd = goodsService.getGoodsAndTypeByLike(keywords, begindate, enddate, page, limit);

			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			return rd;
		}
		

	@PostMapping("goods")
	@ApiOperation(value = "增加商品", httpMethod = "POST", notes = "增加商品", response = AjaxOperateResponse.class)
	public AjaxOperateResponse addGoods(@RequestBody Goods goods) {
		//?这里也要做一些校验和判断，保证传递到sevice层的数据要合法.
		return goodsService.addGoods(goods);
	}

	@PutMapping("goods")
	@ApiOperation(value = "修改商品", httpMethod = "PUT", notes = "修改商品", response = AjaxOperateResponse.class)
	public AjaxOperateResponse updateGoods(@RequestBody Goods goods) {
		return goodsService.updateGoods(goods);
	}

	@DeleteMapping("goods/{goodsid}")
	@ApiOperation(value = "删除商品", httpMethod = "DELETE", notes = "删除商品", response = AjaxOperateResponse.class)
	public AjaxOperateResponse delGoods(
			@ApiParam(name = "goodsid", required = true, value = "删除的id") @PathVariable("goodsid") Integer goodsid) {
		return goodsService.delGoods(goodsid);
	}

	@GetMapping("goods/{goodsid}")
	@ApiOperation(value = "根据id查询", httpMethod = "GET", notes = "查询商品", response = Goods.class)
	public Goods getGoodsById(
			@ApiParam(name = "goodsid", required = true, value = "根据id查询，返回对象信息") @PathVariable("goodsid") Integer goodsid) {

		return goodsService.getGoodsById(goodsid);

	}
}
